package com.test.service.impl;

import com.test.entity.Book;
import com.test.entity.Borrow;
import com.test.entity.User;
import com.test.entity.UserBorrowDetail;
import com.test.mapper.BorrowMapper;
import com.test.service.BorrowService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BorrowServiceImpl implements BorrowService {

    @Resource
    BorrowMapper mapper;

    @Value("${spring.APPURL.userURL}")
    String userURL;

    @Value("${spring.APPURL.bookURL}")
    String bookURL;

    @Override
    public UserBorrowDetail getUserBorrowDetailById(Integer uid) {
        List<Borrow> borrows = mapper.findBorrowByUid(uid);
        RestTemplate template = new RestTemplate();
        User user = template.getForObject(userURL+"/user/"+uid, User.class);
        List<Book> bookList = borrows
                .stream()
                .map(b->template.getForObject(bookURL+"/book/"+b.getBid(),Book.class))
                .collect(Collectors.toList());
        return new UserBorrowDetail(user,bookList);
    }
}
